<?php
	require_once("../conexion/conexion.php");
	require_once("../Funciones/funciones_publicas.php");
	$fun = new funciones_publicas();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Documento sin t&iacute;tulo</title>
<link href="../Estilos/global.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/local.css" rel="stylesheet" type="text/css" />
<link href="../Estilos/CssTabla3.css" rel="stylesheet" type="text/css" />

<script src="../include_js/jquery.js" type="text/javascript"></script>
<style>
.facNormal
{
	background:#D2B48C;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facExenta
{
	background:#81D371;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facElectronica
{
	background:#5F9EA0;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.facCompra
{
	background:#C3D8EB;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
    font-size:11px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
.cuadroNormal {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#D2B48C;
	width:15px;
	height:15px;
}
.cuadroExenta {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#81D371;
	width:15px;
	height:15px;
}
.cuadroElectronica {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#5F9EA0;
	width:15px;
	height:15px;
}
.cuadroCompra {
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	margin-left: 0px;
	padding: 0px;
	border: 1px solid #000000;
	background-color:#C3D8EB;
	width:15px;
	height:15px;
}
#barras {
	padding: 0px;
	margin-top: 5px;
	margin-bottom:0px;
	margin-right:0px;
	margin-left:0px;
}

#barras  .cuadroNormal{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroExenta{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroElectronica{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .cuadroCompra{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 20px;
}
#barras  .espacio{
	padding-top: 0px;
	padding-left: 0px;
	margin: 0px;
	list-style-type: none;
	display: inline;/*deja alineado el menu horizontalmente*/
	padding-right: 210px;
}
.filaSumatoria
{
	background:#CCCCCC;
	font-family:Verdana, Arial, Helvetica, sans-serif; 
	color:#0066CC;
    font-size:12px;
	text-align:center;
	vertical-align:middle;
	padding:3px;
}
</style>
<script>
$(document).ready(function() {
     $("#exportar").click(function(event) {
     //$("#datos_a_enviar").val( $("<div>").append( $("#tbExportar").eq(0).clone()).html());
	 $("#txtDesde").val();
	 $("#txtHasta").val();
	 $("#txtProveedor").val();
     $("#form_compra").submit();
});
});

</script>
</head>

<body>
<form name="form_compra" id="form_compra" method="post" action="Reportes/Excel_Libro_FacturaPagar.php">
<?php
extract($_POST);
	//cliente
 	if ($cmb_Proveedor=='0')
	{
		$cmb_Proveedor="null";
	}
		
	//fecha desde
	if ($txtFIni=='')
	{
		$txtFIni="null";
	}
	else
	{
		$FechaIni = explode("-", $txtFIni);
		$txtFIni = "$FechaIni[2]-$FechaIni[1]-$FechaIni[0]";
	}
	
	//fecha hasta
	if ($txtFFin=='')
	{
		$txtFFin="null";
	}
	else
	{
		$FechaFin = explode("-", $txtFFin);
		$txtFFin = "$FechaFin[2]-$FechaFin[1]-$FechaFin[0]";
	}
	
//factura normal
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacNFecha,'%d/%m/%Y') as fecha,
		FacNNum,
		ProvNombre,
		facturanormal.ProvRut,
		FacNNeto,
		FacNIVA,
		FacNTotal,
		FacNImpEsp,
		date_format(FacNFechaVenc,'%d/%m/%Y') as fechavenc
		
		from facturanormal
		inner join proveedor on
		proveedor.ProvRut=facturanormal.ProvRut
		where 
		(('$cmb_Proveedor' is NULL) or (facturanormal.ProvRut = '".$cmb_Proveedor."')) and
		((($txtFIni is NULL) and ($txtFFin is NULL)) or DATE(FacNFechaVenc) BETWEEN '".$txtFIni."' and '".$txtFFin."') and
		FacNEstado='PENDIENTE'
		order by FacNFechaVenc asc";

//		(($cmb_Mes is NULL) or (month(FacNFecha) = '".$cmb_Mes."')) and
//		(($cmb_Agno is NULL) or (year(FacNFecha) = '".$cmb_Agno."')) and
		
$resultado = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila = mysql_num_rows($resultado);
$con->cerrarBD();
//factura exenta
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacEFecha,'%d/%m/%Y') as fecha,
		FacENum,
		ProvNombre,
		facturaexenta.ProvRut,
		FacETotal,
		date_format(FacEFechaVenc,'%d/%m/%Y') as fechavenc
		
		from facturaexenta
		inner join proveedor on
		proveedor.ProvRut=facturaexenta.ProvRut
		where 
		(('$cmb_Proveedor' is NULL) or (facturaexenta.ProvRut = '".$cmb_Proveedor."')) and
		((($txtFIni is NULL) and ($txtFFin is NULL)) or DATE(FacEFechaVenc) BETWEEN '".$txtFIni."' and '".$txtFFin."') and
		(FacEEstado='PENDIENTE')
		order by FacEFechaVenc asc";

//		(($cmb_Mes is NULL) or (month(FacEFecha) = '".$cmb_Mes."')) and
//		(($cmb_Agno is NULL) or (year(FacEFecha) = '".$cmb_Agno."')) and
		
$resultado2 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila2 = mysql_num_rows($resultado2);
$con->cerrarBD();
//factura electronica
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FacElecFecha,'%d/%m/%Y') as fecha,
		FacElecNum,
		ProvNombre,
		facturaelectronica.ProvRut,
		FacElecNeto,
		FacElecIVA,
		FacElecTotal,
		date_format(FacElecFechaVenc,'%d/%m/%Y') as fechavenc
		
		from facturaelectronica
		inner join proveedor on
		proveedor.ProvRut=facturaelectronica.ProvRut
		where 
		(('$cmb_Proveedor' is NULL) or (facturaelectronica.ProvRut = '".$cmb_Proveedor."')) and
		((($txtFIni is NULL) and ($txtFFin is NULL)) or DATE(FacElecFechaVenc) BETWEEN '".$txtFIni."' and '".$txtFFin."') and
		(FacElecEstado='PENDIENTE')
		order by FacElecFechaVenc asc";

//		(($cmb_Mes is NULL) or (month(FacElecFecha) = '".$cmb_Mes."')) and
//		(($cmb_Agno is NULL) or (year(FacElecFecha) = '".$cmb_Agno."')) and
		
$resultado3 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila3 = mysql_num_rows($resultado3);
$con->cerrarBD();

//factura compra
$con = new Conexion();
$con2 = $con->conexionBD("fpproduccion");
$sql = "select 
		date_format(FCFecha,'%d/%m/%Y') as fecha,
		FCNum,
		ProvNombre,
		fc.ProvRut,
		FCNeto
		
		from faccompra fc
		inner join proveedor p on
		p.ProvRut=fc.ProvRut
		where
		(('$cmb_Proveedor' is NULL) or (fc.ProvRut = '".$cmb_Proveedor."')) and
		((($txtFIni is NULL) and ($txtFFin is NULL)) or DATE(FCFecha) BETWEEN '".$txtFIni."' and '".$txtFFin."') and
		(FCEstado='PENDIENTE')
		order by FCFecha asc";

//		(($cmb_Mes is NULL) or (month(FCFecha) = '".$cmb_Mes."')) and
//		(($cmb_Agno is NULL) or (year(FCFecha) = '".$cmb_Agno."')) and
		
$resultado4 = mysql_query($sql) or die ($con->error_mysql("Error en la consulta".mysql_error()));
$fila4 = mysql_num_rows($resultado4);
$con->cerrarBD();

if(($fila >0)or($fila2>0)or($fila3>0)or($fila4>0))
{
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="uno" id="tbExportar">
<thead>
<tr>
	<td colspan="8"><input name="txtDesde" id="txtDesde" type="hidden" value="<?php echo $txtFIni;?>" /><input name="txtHasta" id="txtHasta" type="hidden" value="<?php echo $txtFFin;?>" /><input type="hidden" id="datos_a_enviar" name="datos_a_enviar" /><input type="hidden" id="txtProveedor" name="txtProveedor" value="<?php echo $cmb_Proveedor;?>" /><div class='option2'>
  <label style="font:Verdana, Arial, Helvetica, sans-serif; color:#0066CC; font-weight:bold;">Reporte Facturas x Pagar</label>
  <div id="barras">
  	<div class="cuadroNormal">&nbsp;</div>&nbsp;
		<label>Factura Normal</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroExenta">&nbsp;</div>&nbsp;
		<label>Factura Exenta</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroElectronica">&nbsp;</div>&nbsp;
		<label>Factura Electronica</label>&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="cuadroCompra">&nbsp;</div>&nbsp;
		<label>Factura Compra</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<div class="espacio"><img src="imagen/icono-excel.gif" width="20" height="20" title="Exportar a Excel" name="exportar" id="exportar" style="cursor: pointer; border: 1px solid green;" onmouseover="this.style.background='black';" onmouseout="this.style.background=''" /></div>
	</div>
</div></td>
</tr>
<tr>
	<th width="11%">f.emision</th>
	<th width="11%">f.venc</th>
	<th width="9%">factura</th>
	<th width="33%">proveedor</th>
	<th width="8%">Imp.Esp</th>
	<th width="9%">neto</th>
	<th width="9%">iva</th>
	<th width="10%">total</th>
</tr>
</thead>
<?php
$sumatoria = 0;
$sumaIVA = 0;
$sumaNeto = 0;
$sumaImpEsp = 0;
//factura normal
while($reg = mysql_fetch_array($resultado))
{
	echo "<tr>";
	echo "<td width=\"11%\" class='facNormal'>".$reg['fecha']."</td>";
	echo "<td width=\"11%\" class='facNormal'>".$reg['fechavenc']."</td>";
	echo "<td width=\"9%\" class='facNormal'>".$reg['FacNNum']."</td>";
	echo "<td width=\"33%\" class='facNormal'>".$reg['ProvNombre']."</td>";
	echo "<td width=\"8%\" class='facNormal'>".$reg['FacNImpEsp']."</td>";
	echo "<td width=\"9%\" class='facNormal'>".number_format ($reg['FacNNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facNormal'>".number_format ($reg['FacNIVA'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facNormal'>".number_format ($reg['FacNTotal'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacNTotal'];
	$sumaIVA += $reg['FacNIVA'];
	$sumaNeto += $reg['FacNNeto'];
	$sumaImpEsp += $reg['FacNImpEsp'];
}
//factura exenta
while($reg = mysql_fetch_array($resultado2))
{
	echo "<tr>";
	echo "<td width=\"11%\" class='facExenta'>".$reg['fecha']."</td>";
	echo "<td width=\"11%\" class='facExenta'>".$reg['fechavenc']."</td>";
	echo "<td width=\"9%\" class='facExenta'>".$reg['FacENum']."</td>";
	echo "<td width=\"33%\" class='facExenta'>".$reg['ProvNombre']."</td>";
	echo "<td width=\"8%\" class='facExenta'>-</td>";
	echo "<td width=\"9%\" class='facExenta'>-</td>";
	echo "<td width=\"9%\" class='facExenta'>-</td>";
	echo "<td width=\"10%\" class='facExenta'>".number_format ($reg['FacETotal'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacETotal'];
}
//factura electronica
while($reg = mysql_fetch_array($resultado3))
{
	echo "<tr>";
	echo "<td width=\"11%\" class='facElectronica'>".$reg['fecha']."</td>";
	echo "<td width=\"11%\" class='facElectronica'>".$reg['fechavenc']."</td>";
	echo "<td width=\"9%\" class='facElectronica'>".$reg['FacElecNum']."</td>";
	echo "<td width=\"33%\" class='facElectronica'>".$reg['ProvNombre']."</td>";
	echo "<td width=\"8%\" class='facElectronica'>-</td>";
	echo "<td width=\"9%\" class='facElectronica'>".number_format ($reg['FacElecNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facElectronica'>".number_format ($reg['FacElecIVA'],0,',','.')."</td>";
	echo "<td width=\"10%\" class='facElectronica'>".number_format ($reg['FacElecTotal'],0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $reg['FacElecTotal'];
	$sumaIVA += $reg['FacElecIVA'];
	$sumaNeto += $reg['FacElecNeto'];
}

//factura compra
while($reg = mysql_fetch_array($resultado4))
{
	$iva = round($reg['FCNeto']*0.19);
	$total = $iva+$reg['FCNeto'];
	echo "<tr>";
	echo "<td width=\"11%\" class='facCompra'>".$reg['fecha']."</td>";
	echo "<td width=\"11%\" class='facCompra'>&nbsp;</td>";
	echo "<td width=\"9%\" class='facCompra'>".$reg['FCNum']."</td>";
	echo "<td width=\"33%\" class='facCompra'>".$reg['ProvNombre']."</td>";
	echo "<td width=\"8%\" class='facCompra'>0</td>";
	echo "<td width=\"9%\" class='facCompra'>".number_format ($reg['FCNeto'],0,',','.')."</td>";
	echo "<td width=\"9%\" class='facCompra'>".number_format ($iva,0,',','.')."</td>";
	echo "<td width=\"10%\" class='facCompra'>".number_format ($total,0,',','.')."</td>";
	echo "</tr>";
	$sumatoria += $total;
	$sumaIVA += $iva;
	$sumaNeto += $reg['FCNeto'];
}
?>
<tr>
	<td colspan="4" class="filaSumatoria">TOTAL</td>
	<td class="filaSumatoria"><?php echo number_format ($sumaImpEsp,0,',','.')?></td>
	<td class="filaSumatoria"><?php echo number_format ($sumaNeto,0,',','.')?></td>
	<td class="filaSumatoria"><?php echo number_format ($sumaIVA,0,',','.')?></td>
	<td class="filaSumatoria"><?php echo number_format ($sumatoria,0,',','.')?></td>
</tr>
</table>
<?php 
}
else
{
    echo "<label style='font-size:16px; color:#CC3333; font-weight:bold;'>No Existen Resultados para la Busqueda</label>";
}
?>
<table>
<tr><td><div id="mensaje"></div></td></tr>
</table>
</form>
</body>
</html>
